# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   selenium_self
# FileName:      read_user_ini.py
# Author:       test_liu
# Datetime:     2024/10/14 下午8:19
# Description: 读取用户配置文件（用例等）
# ---------------------------------------------------------------------------
import os
from configparser import ConfigParser

from WebTest2.WebTest import log


class ReadUserIni:
    def __init__(self, user_dir):
        date_config_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'data_config')
        self.user_data = os.path.join(date_config_path, 'user_config', user_dir)
        user_ini_path = os.path.join(self.user_data, 'config.ini')
        if not os.path.exists(user_ini_path):
            log.error(f'用户配置文件路径{self.user_data}不存在，请注意文件命名')
            raise Exception(f'用户配置文件路径{self.user_data}不存在，请注意文件命名')
        self.config = ConfigParser()
        self.config.read(user_ini_path, encoding='utf-8')

    def get_file_path(self, key):
        try:
            file_name = self.config.get('file', key)
            file_path = os.path.join(self.user_data, file_name)
            return file_path
        except Exception as e:
            log.error(f'方法‘get_file_path’读取配置文件失败，请检查传入的值是否存在于配置文件中，或是否存在该文件，错误为{e}')
            raise e

    def get_table_name(self, key):
        try:
            table_name = self.config.get('table', key)
            return table_name
        except Exception as e:
            log.error(f'方法‘get_table_name’读取配置文件失败，请检查传入的值是否存在于配置文件中，错误为{e}')
            raise e


if __name__ == '__main__':
    read_user_ini = ReadUserIni(user_dir='test_liu')
    print(read_user_ini.get_file_path('excel'))
    print(read_user_ini.get_table_name('sheet1'))
